package com.runa.rccprotocol.dto.equdata;

import com.runa.protocol.util.ByteUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 终端日历时钟
 */
public class TerminalCalendarClock extends EquData{

    private Date date;

    private int second;

    private int minute;

    private int hour;

    private int day;

    private int week;

    private int month;

    private int year;

    public int getSecond() {
        return second;
    }

    public void setSecond(int second) {
        this.second = second;
    }

    public int getMinute() {
        return minute;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getWeek() {
        return week;
    }

    public void setWeek(int week) {
        this.week = week;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public Date toDate() {
        if (this.date == null) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(2000+year, month-1, day, hour, minute, second);
            this.date = calendar.getTime();
        }
        return this.date;
    }

    public byte[] toBytes() {
        byte[] bs = new byte[6];
        bs[0] = (byte) ((second/10<<4) + (second%10));
        bs[1] = (byte) ((minute/10<<4) + (minute%10));
        bs[2] = (byte) ((hour/10<<4) + (hour%10));
        bs[3] = (byte) ((day/10<<4) + (day%10));
        bs[4] = (byte) ((month/10<<4) + (month%10));
        bs[5] = (byte) ((year/10<<4) + (year%10));
        return bs;
    }

    public static TerminalCalendarClock getInstance(byte[] buff, int offset) {
        TerminalCalendarClock clock = new TerminalCalendarClock();
        clock.setSecond(ByteUtil.bcdToInt(buff, offset, 1, false));
        clock.setMinute(ByteUtil.bcdToInt(buff, offset+1, 1, false));
        clock.setHour(ByteUtil.bcdToInt(buff, offset+2, 1, false));
        clock.setDay(ByteUtil.bcdToInt(buff, offset+3, 1, false));
        clock.setWeek(buff[offset+4] >>> 5);
        clock.setMonth(((buff[offset+4]>>4&1) * 10) + (buff[offset+4] & 0x0f));
        clock.setYear(ByteUtil.bcdToInt(buff, offset+5, 1, false));
        return clock;
    }

    public static TerminalCalendarClock getInstance(Date date) {
        TerminalCalendarClock clock = new TerminalCalendarClock();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        clock.setYear(calendar.get(Calendar.YEAR) - 2000);
        clock.setMonth(calendar.get(Calendar.MONTH)+1);
        clock.setDay(calendar.get(Calendar.DATE));
        clock.setHour(calendar.get(Calendar.HOUR_OF_DAY));
        clock.setMinute(calendar.get(Calendar.MINUTE));
        clock.setSecond(calendar.get(Calendar.SECOND));
        clock.date = date;
        return clock;
    }

}
